package domain

import (
	"fmt"
	"time"

	"k8s.io/apimachinery/pkg/util/sets"
)

type CollectedData struct {
	Issue           Issue
	Id              int64
	CveNum          string
	Score           float64
	Version         string
	AffectedProduct []string
	CreateTime      time.Time
}

func (c CollectedData) ToLogString() string {
	return fmt.Sprintf("[%s %s %s]", c.CveNum, c.Issue.Number, c.Issue.Repo)
}

func (c CollectedData) IsCreatedOneYearAgo() bool {
	oneYearAgo := time.Now().AddDate(-1, 0, 0)

	return oneYearAgo.After(c.CreateTime)
}

func (c CollectedData) AffectProductSet() sets.Set[string] {
	return sets.New(c.AffectedProduct...)
}

type CollectedDataSlice []CollectedData

func (s CollectedDataSlice) GroupByBranch(branches []string) map[string]CollectedDataSlice {
	m := make(map[string]CollectedDataSlice)

	for _, branch := range branches {
		var t CollectedDataSlice
		for _, v := range s {
			if v.AffectProductSet().Has(branch) {
				t = append(t, v)
			}
		}

		if len(t) == 0 {
			continue
		}

		m[branch] = t
	}

	return m
}
